#include <string.h>
#include <stdio.h>

#include <mysql/mysql.h>

#define STRING_SIZE 50

#define SELECT_SAMPLE "SELECT xx, yy, zz, tt FROM nemesis_db_test"

#define MAXCOLUMN 4

MYSQL* mysql;
MYSQL_STMT    *stmt;
MYSQL_BIND    bind[MAXCOLUMN];
MYSQL_RES     *prepare_meta_result;
MYSQL_TIME    ts;
unsigned long length[MAXCOLUMN];
int           param_count, column_count, row_count;
float         float_data;
int           int_data;
char          str_data[STRING_SIZE];
my_bool       is_null[MAXCOLUMN];

/*
 * From http://dev.mysql.com/doc/refman/4.1/en/mysql-stmt-fetch.html
 */
int main () 
{
   if ((mysql = mysql_init (NULL)) == NULL)
      exit (-12);
   
   if (mysql_real_connect (mysql, NULL, "sdp", "sdp", "test", 0, NULL, 0L) == NULL) {
      fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
      fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
      exit(0);
   }   
   
   /* Prepare a SELECT query to fetch data from test_table */
   stmt = mysql_stmt_init(mysql);
   if (!stmt)
   {
     fprintf(stderr, " mysql_stmt_init(), out of memory\n");
     exit(0);
   }
   
   if (mysql_stmt_prepare(stmt, SELECT_SAMPLE, strlen(SELECT_SAMPLE)))
   {
     fprintf(stderr, " mysql_stmt_prepare(), SELECT failed\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }
   fprintf(stdout, " prepare, SELECT successful\n");

   /* Get the parameter count from the statement */
   param_count= mysql_stmt_param_count(stmt);
   fprintf(stdout, " total parameters in SELECT: %d\n", param_count);

   if (param_count != 0) /* validate parameter count */
   {
     fprintf(stderr, " invalid parameter count returned by MySQL\n");
     exit(0);
   }

   /* Fetch result set meta information */
   prepare_meta_result = mysql_stmt_result_metadata(stmt);
   if (!prepare_meta_result)
   {
     fprintf(stderr," mysql_stmt_result_metadata(), returned no meta information\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }

   /* Get total columns in the query */
   column_count= mysql_num_fields(prepare_meta_result);
   fprintf(stdout, " total columns in SELECT statement: %d\n", column_count);

   if (column_count != MAXCOLUMN) /* validate column count */
   {
     fprintf(stderr, " invalid column count returned by MySQL\n");
     exit(0);
   }

   /* Execute the SELECT query */
   if (mysql_stmt_execute(stmt))
   {
     fprintf(stderr, " mysql_stmt_execute(), failed\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }

   /* Bind the result buffers for all 4 columns before fetching them */

   memset(bind, 0, sizeof(bind));

   /* INTEGER COLUMN */
   bind[0].buffer_type= MYSQL_TYPE_LONG;
   bind[0].buffer= (char *)&int_data;
   bind[0].is_null= &is_null[0];
   bind[0].length= &length[0];

   /* STRING COLUMN */
   bind[1].buffer_type= MYSQL_TYPE_STRING;
   bind[1].buffer= (char *)str_data;
   bind[1].buffer_length= STRING_SIZE;
   bind[1].is_null= &is_null[1];
   bind[1].length= &length[1];

   /* SMALLINT COLUMN */
   bind[2].buffer_type= MYSQL_TYPE_FLOAT;
   bind[2].buffer= (char *)&float_data;
   bind[2].is_null= &is_null[2];
   bind[2].length= &length[2];

   /* TIMESTAMP COLUMN */
   bind[3].buffer_type= MYSQL_TYPE_TIMESTAMP;
   bind[3].buffer= (char *)&ts;
   bind[3].is_null= &is_null[3];
   bind[3].length= &length[3];

   /* Bind the result buffers */
   if (mysql_stmt_bind_result(stmt, bind))
   {
     fprintf(stderr, " mysql_stmt_bind_result() failed\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }

   /* Now buffer all results to client (optional step) */
   if (mysql_stmt_store_result(stmt))
   {
     fprintf(stderr, " mysql_stmt_store_result() failed\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }

   /* Fetch all rows */
   row_count= 0;
   fprintf(stdout, "Fetching results ...\n");
   while (!mysql_stmt_fetch(stmt))
   {
     row_count++;
     fprintf(stdout, "  row %d\n", row_count);

     /* column 1 */
     fprintf(stdout, "   column1 (integer)  : ");
     if (is_null[0])
       fprintf(stdout, " NULL\n");
     else
       fprintf(stdout, " %d(%ld)\n", int_data, length[0]);

     /* column 2 */
     fprintf(stdout, "   column2 (string)   : ");
     if (is_null[1])
       fprintf(stdout, " NULL\n");
     else
       fprintf(stdout, " %s(%ld)\n", str_data, length[1]);

     /* column 3 */
     fprintf(stdout, "   column3 (float) : ");
     if (is_null[2])
       fprintf(stdout, " NULL\n");
     else
       fprintf(stdout, " %f(%ld)\n", float_data, length[2]);

     /* column 4 */
     fprintf(stdout, "   column4 (timestamp): ");
     if (is_null[3])
       fprintf(stdout, " NULL\n");
     else
       fprintf(stdout, " %04d-%02d-%02d %02d:%02d:%02d (%ld)\n",
                        ts.year, ts.month, ts.day,
                        ts.hour, ts.minute, ts.second,
                        length[3]);
     fprintf(stdout, "\n");
   }

   /* Free the prepared result metadata */
   mysql_free_result(prepare_meta_result);

   /* Close the statement */
   if (mysql_stmt_close(stmt))
   {
     fprintf(stderr, " failed while closing the statement\n");
     fprintf(stderr, " %s\n", mysql_stmt_error(stmt));
     exit(0);
   }

}
